package io.bdmc.modules.bss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.bdmc.common.utils.MyPage;
import io.bdmc.modules.bss.entity.DeviceMeasure;
import io.bdmc.modules.bss.mapper.DeviceMeasureMapper;
import io.bdmc.modules.bss.model.DeviceMeasureParam;
import io.bdmc.modules.bss.service.IDeviceMeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 设备测试 服务实现类
 * </p>
 *
 * @author ping
 * @since 2020-06-30
 */
@Service
public class DeviceMeasureServiceImpl extends ServiceImpl<DeviceMeasureMapper, DeviceMeasure>
        implements IDeviceMeasureService {

    @Autowired
    DeviceMeasureMapper _devicemeasureMapper;

    @Override
    public MyPage<DeviceMeasure> mySelectPage(MyPage<DeviceMeasure> myPage, DeviceMeasureParam params) {
        return _devicemeasureMapper.mySelectPage(myPage, params);
    }

    @Override
    public MyPage<DeviceMeasure> mySelectPageFor1U(MyPage<DeviceMeasure> myPage, DeviceMeasureParam params) {
        return _devicemeasureMapper.mySelectPageFor1U(myPage, params);
    }

    @Override
    public MyPage<DeviceMeasure> mySelectPage1(MyPage<DeviceMeasure> myPage, DeviceMeasureParam params) {
        return _devicemeasureMapper.mySelectPage1(myPage, params);
    }

    @Override
    public List<HashMap<String, Object>> selectForLine(DeviceMeasureParam param) {
        final List<HashMap<String, Object>> hashMaps = _devicemeasureMapper.selectForLine(param);
        return hashMaps;
    }

    @Transactional
    @Override
    public Boolean setStandardMeasure(DeviceMeasure standMeasure) {
        DeviceMeasure oldStands = getOne(new QueryWrapper<DeviceMeasure>().eq("device_sn", standMeasure.getDeviceSn())
                .eq("channel", standMeasure.getChannel()).eq("standard", 1).last("limit 1"));
        if (oldStands != null) {
            oldStands.setStandard(false);
            oldStands.updateById();
        }

        standMeasure.setStandard(true);
        return standMeasure.updateById();
    }

    @Override
    public DeviceMeasure getStandardByRouteAndChannel(Integer routeId, Integer channel) {
        return getOne(new QueryWrapper<DeviceMeasure>().eq("route_id", routeId).eq("standard", 1)
                        .eq("channel", channel).last("limit 1"));
    }

    @Override
    public List<DeviceMeasure> exportList(DeviceMeasureParam params) {
        return _devicemeasureMapper.mySelectPage(params);
    }

    @Override
    public List<DeviceMeasure> exportListFor1U(DeviceMeasureParam params) {
        return _devicemeasureMapper.mySelectPageFor1U(params);
    }
}
